import java.util.List;

public class GetRedPacket extends Thread {
    private final List<Integer> redPacket;
    public GetRedPacket(List<Integer> redPacket, String name) {
        super(name);
        this.redPacket = redPacket;
    }

    public void  run() {
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (redPacket) {
                if (redPacket.isEmpty()){
                    break;
                }
                int index = (int) (Math.random() * redPacket.size());
                Integer money = redPacket.remove(index);
                System.out.println(name + " 抢到了 " + money + " 元");
                if (redPacket.isEmpty()) {
                    System.out.println("结束了");
                    System.out.println("剩余 " + redPacket.size() + " 元");
                    break;
                }

            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
